// SPDX-License-Identifier: GPL-2.0
#include "ax_utils.h"


#include <linux/mutex.h>
int axvu_mutex_init(os_mutex_t *mutex)
{
	struct mutex *p = NULL;

	if (mutex == NULL) {
		ax_err(1, "%s - parameter invalid!\n", __func__);
		return -1;
	}
	p = axvu_kmalloc(sizeof(struct mutex), os_gfp_kernel);
	if (p == NULL) {
		ax_err(1, "%s - axvu_kmalloc error!\n", __func__);
		return -1;
	}
	mutex_init(p);
	mutex->mutex = p;

	return 0;
}

int axvu_mutex_lock(os_mutex_t *mutex)
{
	struct mutex *p = NULL;


	if (mutex == NULL) {
		ax_err(1, "%s - parameter invalid!\n", __func__);
		return -1;
	}
	p = (struct mutex *)(mutex->mutex);
	mutex_lock(p);

	return 0;
}

int axvu_mutex_lock_interruptible(os_mutex_t *mutex)
{
	struct mutex *p = NULL;

	if (mutex == NULL) {
		ax_err(1, "%s - parameter invalid!\n", __func__);
		return -1;
	}
	p = (struct mutex *)(mutex->mutex);
	return mutex_lock_interruptible(p);
}

int axvu_mutex_trylock(os_mutex_t *mutex)
{
	struct mutex *p = NULL;

	if (mutex == NULL) {
		ax_err(1, "%s - parameter invalid!\n", __func__);
		return -1;
	}
	p = (struct mutex *)(mutex->mutex);

	return mutex_trylock(p);
}

void axvu_mutex_unlock(os_mutex_t *mutex)
{
	struct mutex *p = NULL;

	p = (struct mutex *)(mutex->mutex);

	mutex_unlock(p);
}

void axvu_mutex_destroy(os_mutex_t *mutex)
{
	struct mutex *p = NULL;

	if (mutex == NULL) {
		ax_err(1, "%s - parameter invalid!\n", __func__);
	} else {
		p = (struct mutex *)(mutex->mutex);
		axvu_kfree(p);
		mutex->mutex = NULL;
	}
}
